package com.yangji.uemgupload;


import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;

import java.lang.reflect.Method;

public class UMengStatistics {


    private static int IS_UM_INIT_SUCCESS = 0;//0未初始化 1 成功 2 失败
    private static final String UMENG_CLASS_NAME = "com.umeng.analytics.MobclickAgent";


    public static void onPause(Activity activity) {
        if (IS_UM_INIT_SUCCESS == 2) {
            return;
        }
        try {
            Class<?> testClass = Class.forName(UMENG_CLASS_NAME);
            Method saddMethod2 = testClass.getMethod("onPause", new Class[]{Context.class});
            saddMethod2.invoke(null, new Object[]{activity});
            IS_UM_INIT_SUCCESS = 1;
        } catch (Exception e) {
            IS_UM_INIT_SUCCESS = 2;
            System.err.println("友盟统计失败");
        }
    }

    public static void onResume(Activity activity) {
        if (IS_UM_INIT_SUCCESS == 2) {
            return;
        }
        try {
            Class<?> testClass = Class.forName(UMENG_CLASS_NAME);
            Method saddMethod2 = testClass.getMethod("onResume", new Class[]{Context.class});
            saddMethod2.invoke(null, new Object[]{activity});
            IS_UM_INIT_SUCCESS = 1;
        } catch (Exception e) {
            IS_UM_INIT_SUCCESS = 2;
            System.err.println("友盟统计失败");
        }
    }


    public static void onResume(Fragment name) {
        if (IS_UM_INIT_SUCCESS == 2) {
            return;
        }
        try {
            Class<?> testClass = Class.forName(UMENG_CLASS_NAME);
            Method saddMethod2 = testClass.getMethod("onPageStart", new Class[]{String.class});
            saddMethod2.invoke(null, new Object[]{name.getClass().getName()});
            IS_UM_INIT_SUCCESS = 1;
        } catch (Exception e) {
            IS_UM_INIT_SUCCESS = 2;
            System.err.println("友盟统计失败");
        }
    }

    public static void onPause(Fragment name) {
        if (IS_UM_INIT_SUCCESS == 2) {
            return;
        }
        try {
            Class<?> testClass = Class.forName(UMENG_CLASS_NAME);
            Method saddMethod2 = testClass.getMethod("onPageEnd", new Class[]{String.class});
            saddMethod2.invoke(null, new Object[]{name.getClass().getName()});
            IS_UM_INIT_SUCCESS = 1;
        } catch (Exception e) {
            IS_UM_INIT_SUCCESS = 2;
            System.err.println("友盟统计失败");
        }
    }

    private static Context context;

    public static void setContext(Context context) {
        UMengStatistics.context = context;
    }

    public static void statistics(String name) {
        if (IS_UM_INIT_SUCCESS == 2) {
            return;
        }
        try {
            Class<?> testClass = Class.forName(UMENG_CLASS_NAME);
            Method saddMethod2 = testClass.getMethod("onEvent", new Class[]{Context.class, String.class});
            saddMethod2.invoke(null, new Object[]{context, name});
            IS_UM_INIT_SUCCESS = 1;
        } catch (Exception e) {
            IS_UM_INIT_SUCCESS = 2;
            System.err.println("友盟统计失败");
        }
    }


}
